package org.ilead.iserve.dao.impl;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.ilead.iserve.dao.INewsDAO;
import org.ilead.iserve.model.News;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 	* A data access object (DAO) providing persistence and search support for News entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see org.ilead.iserve.model.News
  * @author MyEclipse Persistence Tools 
 */

public class NewsDAO extends HibernateDaoSupport implements INewsDAO  {
	     private static final Logger log = LoggerFactory.getLogger(NewsDAO.class);
		protected void initDao() {
		//do nothing
	}
    
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#save(org.ilead.iserve.model.News)
	 */
    @Override
	public void save(News transientInstance) {
        log.debug("saving News instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#delete(org.ilead.iserve.model.News)
	 */
	@Override
	public void delete(News persistentInstance) {
        log.debug("deleting News instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findById(java.lang.Long)
	 */
    @Override
	public News findById( java.lang.Long id) {
        log.debug("getting News instance with id: " + id);
        try {
            News instance = (News) getHibernateTemplate()
                    .get("org.ilead.iserve.model.News", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByExample(org.ilead.iserve.model.News)
	 */
    @Override
	public List findByExample(News instance) {
        log.debug("finding News instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
    @Override
    public List findByProperty(String propertyName, Object value) {
    	return findByProperty(propertyName, value, "");
    }
    
	public List findByProperty(String propertyName, Object value, String orderBy) {
      log.debug("finding News instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from News as model where model." 
         						+ propertyName + "=? " + orderBy;
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByCategoryId(java.lang.Object)
	 */
	@Override
	public List findByCategoryId(Object categoryId) {
		return findByProperty(CATEGORY_ID, categoryId, "order by model.gmtModify desc");
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByTitle(java.lang.Object)
	 */
	@Override
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByImage(java.lang.Object)
	 */
	@Override
	public List findByImage(Object image
	) {
		return findByProperty(IMAGE, image
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByOutline(java.lang.Object)
	 */
	@Override
	public List findByOutline(Object outline
	) {
		return findByProperty(OUTLINE, outline
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByKeyWords(java.lang.Object)
	 */
	@Override
	public List findByKeyWords(Object keyWords
	) {
		return findByProperty(KEY_WORDS, keyWords
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByOperator(java.lang.Object)
	 */
	@Override
	public List findByOperator(Object operator
	) {
		return findByProperty(OPERATOR, operator
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByAuthor(java.lang.Object)
	 */
	@Override
	public List findByAuthor(Object author
	) {
		return findByProperty(AUTHOR, author
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByContent(java.lang.Object)
	 */
	@Override
	public List findByContent(Object content
	) {
		return findByProperty(CONTENT, content
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByAuditor(java.lang.Object)
	 */
	@Override
	public List findByAuditor(Object auditor
	) {
		return findByProperty(AUDITOR, auditor
		);
	}
	
	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findByStatus(java.lang.Object)
	 */
	@Override
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	/* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all News instances");
		try {
			String queryString = "from News";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#merge(org.ilead.iserve.model.News)
	 */
    @Override
	public News merge(News detachedInstance) {
        log.debug("merging News instance");
        try {
            News result = (News) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#attachDirty(org.ilead.iserve.model.News)
	 */
    @Override
	public void attachDirty(News instance) {
        log.debug("attaching dirty News instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see org.ilead.iserve.dao.impl.INewsDAO#attachClean(org.ilead.iserve.model.News)
	 */
    @Override
	public void attachClean(News instance) {
        log.debug("attaching clean News instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static INewsDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (INewsDAO) ctx.getBean("NewsDAO");
	}
}